using UnityEngine;
using UnityEngine.EventSystems;
using System;

public class BuildingManager : MonoBehaviour
{
    private BuildingTypeList buildingTypeList; // 建筑类型列表对象
    private BuildingType activeBuildingType; // 当前选中的建筑类型对象

    [SerializeField]private Building hqBuilding;//总部Building组件

    private Camera mainCamera;

    public static BuildingManager Instance {get;private set;}

    public event EventHandler<OnActiveBuildingTypeChangedEventArgs> OnActiveBuildingTypeChanged;
    public class OnActiveBuildingTypeChangedEventArgs : EventArgs{
        public BuildingType activeBuildingType;
    }

    void Awake(){
        Instance = this;

        buildingTypeList = Resources.Load<BuildingTypeList>("ScriptableObject/建筑类型/建筑类型列表"); // 加载建筑类型列表
        activeBuildingType = buildingTypeList.buildingTypeList[0]; // 初始化为列表中的第一个建筑类型
    }

    private void Start()
    {
        mainCamera = Camera.main; // 获取主摄像机对象
        hqBuilding.GetComponent<HealthSystem>().OnDied += HQ_OnDied;
    }

    private void HQ_OnDied(object sender,EventArgs e){
        GameOverUI.Instance.Show();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            if(activeBuildingType.prefab != null){
                if(CanSpawnBuilding(activeBuildingType, UtilsClass.GetMouseWorldPosition(), out string errorMessage)){
                    if (ResourceManager.Instance.CanAfford(activeBuildingType.constructionResourceCostArray)){
                        ResourceManager.Instance.SpendResources(activeBuildingType.constructionResourceCostArray);
                        // 在鼠标点击位置创建一个建筑实例
                        // Instantiate(activeBuildingType.prefab, UtilsClass.GetMouseWorldPosition(), Quaternion.identity);
                        BuildingConstruction.Create(UtilsClass.GetMouseWorldPosition(), activeBuildingType);
                    }else{
                        TooltipUI.Instance.Show("资源不够 " + activeBuildingType.GetConstructionResourceCoststring(), new TooltipUI.TooltipTimer{timer = 2f});
                    }
                }else{
                    TooltipUI.Instance.Show(errorMessage, new TooltipUI.TooltipTimer{timer = 2f});
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.T)){
            Vector3 enemySpawnPosition = UtilsClass.GetMouseWorldPosition() + UtilsClass.GetRandomDir() * 5f;
            Enemy.Create(enemySpawnPosition);
        }
    }

    // 修改当前选中的建筑类型对象
    public void SetActiveBuildingType(BuildingType buildingType){
        activeBuildingType = buildingType;

        OnActiveBuildingTypeChanged?.Invoke(this,new OnActiveBuildingTypeChangedEventArgs {activeBuildingType = activeBuildingType});
    }

    //获取选中的建筑类型
    public BuildingType GetActiveBuildingType(){
        return activeBuildingType;
    }

    /// <summary>
    /// 检查是否可以在给定位置生成建筑物
    /// </summary>
    /// <param name="buildingType">要生成的建筑物类型</param>
    /// <param name="position">生成建筑物的位置</param>
    /// <returns>如果可以生成建筑物，则返回 true，否则返回 false</returns>
    private bool CanSpawnBuilding(BuildingType buildingType, Vector3 position, out string errorMessage)
    {
        // 获取建筑物预制体的碰撞器
        BoxCollider2D boxCollider2D = buildingType.prefab.GetComponent<BoxCollider2D>();

        // 在指定位置使用盒形检测获取所有重叠的碰撞体
        Collider2D[] collider2DArray = Physics2D.OverlapBoxAll(position + (Vector3)boxCollider2D.offset, boxCollider2D.size, 0);

        // 判断是否有其他碰撞体与要生成的建筑物重叠，如果有则返回 false
        bool isAreaClear = collider2DArray.Length == 0;
        if (!isAreaClear)
        {
            errorMessage = "区域重叠!";
            return false;
        }
        errorMessage = "";
        // 如果以上条件都满足，则可以生成建筑物，返回 true
        return true;
    }

    public Building GetHQBuilding(){
        return hqBuilding;
    } 
}